package enron;

import java.util.ArrayList;

import java.awt.Component;
import javax.swing.AbstractCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComponent;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableCellEditor;

public class ComboBoxTableEditor extends AbstractCellEditor implements
		TableCellEditor {
	private static final long serialVersionUID = 8252373470100465870L;
	protected TableCellEditor delegate;
	private JComboBox comboBox = new JComboBox();
	//private JLabel outerLabel = new JLabel();

	// Initializes the comboBox.
	public ComboBoxTableEditor(ArrayList<?> data, TableCellEditor defaultEditor) {
		delegate = defaultEditor;
		comboBox.setName("Table.comboBoxCellEditor");
		comboBox.setModel(new DefaultComboBoxModel(data.toArray()));
		comboBox.putClientProperty(this.getClass(), this);
		comboBox.setBorder(new EmptyBorder(0,0,0,0));
		//((JComponent) comboBox.getEditor().getEditorComponent()).setBorder(new EmptyBorder(0,0,0,0));
		//((JComponent) comboBox.getRenderer().getListCellRendererComponent(new JList(comboBox.getModel()), comboBox.getSelectedItem(), comboBox.getSelectedIndex(), true, comboBox.hasFocus())).setBorder(new EmptyBorder(0,0,0,0));
		comboBox.setPrototypeDisplayValue("BlahBlahBlahBlahBlah");
		comboBox.setMinimumSize(comboBox.getPreferredSize());
		//comboBox.setBorder(new EmptyBorder(0,0,0,0));
		//outerLabel.add(comboBox);
	}

	// Prepares the comboBox component and returns it.
	public Component getTableCellEditorComponent(JTable table, Object value,
			boolean isSelected, int row, int column) {
		JComponent c = (JComponent)delegate.getTableCellEditorComponent(table, value, 
				isSelected, row, column);
		//respect insets and focus
		//comboBox.setBorder(c.getBorder());
		//copy opacity
		comboBox.setOpaque(c.isOpaque());
		//apply background
		comboBox.setBackground(c.getBackground());    
		//apply foreground
		//((JComponent) comboBox.getEditor()).setForeground(c.getForeground());
		if(value == null) {
			comboBox.setSelectedIndex(0);
		} else {
			comboBox.setSelectedItem(value);
		}
		//return comboBox;
		return comboBox;
	}

	@Override
	public Object getCellEditorValue() {
		return comboBox.getSelectedItem();
	}
}
